/**
 * 
 */
package com.example.com.baruffini_mele.booking4u;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import android.database.Cursor;

/**
 * @author carmencristianabaruffini
 *
 */
public class GCalendar {
	private static final String TAG = "GCalendar";	


	private int id;
	private String calendarId;
	private String name;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the calendarId
	 */
	public String getCalendarId() {
		return calendarId;
	}
	/**
	 * @param calendarId the calendarId to set
	 */
	public void setCalendarId(String calendarId) {
		this.calendarId = calendarId;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	public GCalendar() {
		super();
	}
	public GCalendar(String calendarId, String name, int id) {
		super();
		this.calendarId = calendarId;
		this.name = name;
		this.id = id;
	}
	
	public static ArrayList postCalendarsRequest() {
		String result = ""; //result obtained from remote query   
		String id = "";
		String calendarId = "";
		String name = "";
		//String urlString = Resources.getSystem().getString((R.string.URL_g_calendars_list));
		String urlString = "http://143.225.162.99/~carmen/dieti/ws/g_calendars_list.php";
		ArrayList<GCalendar> calendarsList = new ArrayList<GCalendar>();

		
//http post
		  		
		try{
			
		//Create  HttpClient and HttpPost objects to execute the POST request
		HttpClient httpclient = new DefaultHttpClient();
		
		HttpPost httppost = new HttpPost(urlString);
		//Set POST data
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		//nameValuePairs.add(new BasicNameValuePair("email",userEmail));
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		//Execute the POST request
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		InputStream is = entity.getContent();
		
		
		
		//convert response to string
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
		     sb.append(line + "\n");
		}
		is.close();
		
		result=sb.toString();

		 } 
		catch(Exception e){
			e.printStackTrace();
		}

		//parse json data
		try{
		JSONArray jArray = new JSONArray(result);
		for(int i=0;i<jArray.length();i++){
		        JSONObject json_data = jArray.getJSONObject(i);
		        name = json_data.getString("name");
		        id = json_data.getString("id");
		        calendarId = json_data.getString("calendar_id");
		        
		//GCalendar gc = new GCalendar(calendarId, name, Integer.parseInt(id));     
		calendarsList.add(new GCalendar(calendarId, name, Integer.parseInt(id)));
		        
		}
		
		}catch(JSONException e){
		Log.e(TAG, "Error parsing data "+e.toString());
		}
		return calendarsList;
    
    }
	
	
	
	

}
